package com.example.komunikator;

import java.security.Policy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import android.content.Context;

import org.jivesoftware.smack.ConnectionConfiguration;

import android.os.Message;
import android.os.StrictMode;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import com.example.komunikator.R;
import com.example.komunikator.R.id;
import com.example.komunikator.R.layout;

import android.widget.Button;
import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.beans.*;

public class SettingsDialog extends Dialog implements android.view.View.OnClickListener {
	    private LoginActivity xmppClient;
//	    private static final String url = "jdbc:mysql://mysql.agh.edu.pl:3306/rybczyns";
//	    private static final String user = "rybczyns";
//	    private static final String pass = "6gtZxjms";
		
	    

	    
	 
	    public SettingsDialog(LoginActivity xmppClient) {
	        super(xmppClient);
	        this.xmppClient = xmppClient;
	    }
	 
	    protected void onStart() {
		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		    StrictMode.setThreadPolicy(policy);//ThreadPolicy(policy);
		    
		    
	        super.onStart();
	        setContentView(R.layout.settings);
	

	        
	        
	        
	        
	        getWindow().setFlags(4, 4);
	        setTitle("XMPP Settings");
	        Button ok = (Button) findViewById(R.id.okSettingButton);
	        ok.setOnClickListener(this);
	    }
	 
	    public void onClick(View v) {
	    	//TODO: formularz z wpisywanymi danymi.
	        String host = getText(R.id.hostSetting);
	        host="talk.google.com";
	        String port = getText(R.id.portSetting);
	        port="5222";
	       // String service = getText(R.id.service);
	        String service = "gmail.com";
	        String username = getText(R.id.idSetting);
	        username="agh.communicator@gmail.com";
	        String password = getText(R.id.loginTextBox);
	        //password="!Start123";
	        
//	        try {  
//				Class.forName("com.mysql.jdbc.Driver");  
//				Connection con = DriverManager.getConnection(url, user, pass); 
//				SQLLogic.getInstance().setConnection(con);
////				Statement st = con.createStatement();  
////				ResultSet rs = st.executeQuery("select * from Users");  
//
//
//
//				  
//
//			} catch (NullPointerException e) { 
//				e.printStackTrace();
//			} catch (ClassNotFoundException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (java.sql.SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} finally{
//
//
//			}  
	        
	         
	        
	        
	        
	 
	        // Create a connection
	        ConnectionConfiguration connConfig =
	                new ConnectionConfiguration(host, Integer.parseInt(port), service);
	        XMPPConnection connection = new XMPPConnection(connConfig);
	        XMPPLogic.getInstance().setConnection(connection);
	        
	        
	        
	        
	        

	        try {
	            connection.connect();
	            Log.i("XMPPClient", "[SettingsDialog] Connected to " + connection.getHost());
	        } catch (XMPPException ex) {
	        	
	            Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " + connection.getHost());
	            xmppClient.setConnection(null);
	            
	            int duration = Toast.LENGTH_SHORT;
	            Toast toast = Toast.makeText(this.getContext(), "Nie udalo sie polaczyc!", duration);
	            toast.show();
	            
	        }
	        try {
	            connection.login(username, password);
	            Log.i("XMPPClient", "Logged in as " + connection.getUser());
	 
	            // Set the status to available
	            Presence presence = new Presence(Presence.Type.available);
	            connection.sendPacket(presence);
	            xmppClient.setConnection(connection);
	        } catch (XMPPException ex) {
	            Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " + username);
	            xmppClient.setConnection(null);
	        }
	        
	        
	        
	        
	        
	        
	        
	        
	        
	        
	        
	        dismiss();
	    }
	 
	    private String getText(int id) {
	        EditText widget = (EditText) this.findViewById(id);
	        return widget.getText().toString();
	    }
	}